package com.webreport.utils;

import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;

import com.webreport.model.QueryModel;

public class DaoUtils {
	public static List querySQL(HibernateTemplate hibernateTemplate, String sqlinput, Map paramsinput, int startRowsinput, int rowsPerPageinput){
		return query(hibernateTemplate, false, sqlinput, paramsinput, startRowsinput, rowsPerPageinput);
	}
	public static List querySQL(HibernateTemplate hibernateTemplate, String sqlinput, Map paramsinput){
		return query(hibernateTemplate, false, sqlinput, paramsinput, 0, 0);
	}
	public static List querySQL(HibernateTemplate hibernateTemplate, String sqlinput){
		return query(hibernateTemplate, false, sqlinput, null, 0, 0);
	}
	public static List queryHQL(HibernateTemplate hibernateTemplate, String sqlinput, Map paramsinput, int startRowsinput, int rowsPerPageinput){
		return query(hibernateTemplate, true, sqlinput, paramsinput, startRowsinput, rowsPerPageinput);
	}
	public static List queryHQL(HibernateTemplate hibernateTemplate, String sqlinput, Map paramsinput){
		return query(hibernateTemplate, true, sqlinput, paramsinput, 0, 0);
	}
	public static List queryHQL(HibernateTemplate hibernateTemplate, String sqlinput){
		return query(hibernateTemplate, true, sqlinput, null, 0, 0);
	}
	public static List queryHQL(HibernateTemplate hibernateTemplate, QueryModel query){
		return query(hibernateTemplate, true, query.getSql(), query.getParams(), query.getStartrow(), query.getSizerow());
	}
	public static long queryHQLCount(HibernateTemplate hibernateTemplate, QueryModel query){
		List list=  query(hibernateTemplate, true, query.getSqlCount(), query.getParams(), query.getStartrow(), query.getSizerow());
		if (list != null && list.size() ==1){
			return (Long)list.get(0);
		}
		return 0;
	}
	@SuppressWarnings("rawtypes")
	private static List query(HibernateTemplate hibernateTemplate, boolean isHql, String sqlinput, Map paramsinput, int startRowsinput, int rowsPerPageinput) {
		@SuppressWarnings("unchecked")
		final boolean ishql = isHql;
		final String sql = sqlinput;
		final Map params = paramsinput;
		final int rowsPerPage = rowsPerPageinput;
		final int startRows = startRowsinput;
		@SuppressWarnings("unchecked")
		List list = hibernateTemplate.execute(new HibernateCallback() {
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query queryObject = null;
				if (ishql) {
					queryObject = session.createQuery(sql.toString());
				} else {
					queryObject = session.createSQLQuery(sql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);
				}
				if (params != null) {
					for (Iterator ite = params.keySet().iterator(); ite.hasNext();) {
						String paramName = (String) ite.next();
						queryObject.setParameter(paramName, params.get(paramName));
					}
				}
				if (rowsPerPage != 0) {
					
					queryObject.setFirstResult(startRows);
					queryObject.setMaxResults(rowsPerPage);
				}
				return queryObject.list();
			}
		});
		return list;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static int update(HibernateTemplate template,
			String sql,Map params) {
		if (sql != null){
			final String updatesql = sql;
			final Map<String,Object> fparams =params;
			int l = template.execute(new HibernateCallback() {
				public Object doInHibernate(Session session)
						throws HibernateException, SQLException {
					Query updateObject = null;
					updateObject = session.createQuery(updatesql);
					if (fparams != null) {
						for (Iterator ite = fparams.keySet().iterator(); ite
								.hasNext();) {
							String paramName = (String) ite.next();
							updateObject.setParameter(paramName,
									fparams.get(paramName));
						}
					}
					return updateObject.executeUpdate();
				}
			});
			return l;
		}else{
			return 0;
		}
	}
}
